﻿var app = angular.module('app', ['angular.jquery']);
var item = null;
var flag = false;
var objCurrent = null;
app.controller('DemoCtrl', [
    '$scope', '$http',
    'AppDialogService',
function ($scope, $http, AppDialogService) {
    'use strict';        
    objCurrent = Editor.getItem("ban");    
    $scope.showAdd = function () {
        if (objCurrent != null) {
            return objCurrent.isadd;
        }
        return false;
    }
    $scope.showEdit = function () {
        if (objCurrent != null) {
            return objCurrent.edit;
        }
        return false;
    }
    $scope.showDelete = function () {
        if (objCurrent != null) {
            return objCurrent.isdelete;
        }
        return false;
    }

    $http.get(Editor.Url + "/ban/GetAll")
     .success(function (response) {
         $scope.tasks = response;
         console.dir(response);
     });

    $http.get(url + "/vu/GetAll")
    .success(function (response) {
        $scope.vus = response;
    });

    $scope.loadData = function () {
        $http.get(Editor.Url + "/ban/GetAll")
                .success(function (response) {
                    $scope.tasks = response;
                });
    }

    $scope.delete = function (id) {
        var a = confirm("Bạn có thực sự muốn xóa dòng này?");
        if (a === false)
            return;
        $http.delete(Editor.Url + "/api/BanAPI/" + id).success(function (data, status, headers) {
            $scope.loadData();
        });
    }

    $scope.appDialog = function (_flag, _item) {
        item = _item;
        flag = _flag;
        AppDialogService.openDialog({
            testValue: 'application'
        }).then(function (result) {
            if (result.ok) {
                var mota = $("#mOTABAN").val();
                result.obj.mOTABAN = mota;
                if (flag) {
                    $http.put(Editor.Url + "/api/BanAPI/" + result.obj.iD, result.obj).success(function (data, status, headers) {
                        if (status === 200) {
                            alert("cập nhập thành công");
                        }
                    });
                }
                else {
                    $http.post(Editor.Url + "/api/BanAPI", result.obj).success(function (data, status, headers) {
                        $scope.loadData();
                    });
                }
            }
        });

    };

}]);

app.controller('DialogCtrl', [
    '$scope',
    'AppDialogService',
function ($scope, AppDialogService) {
    'use strict';

    $scope.obj = null;

    $scope.onOpen = function () {
        $scope.obj = item;
        if ($scope.obj != null) {
            $('#mOTABAN').val($scope.obj.mOTABAN).blur();
        }
    };

    $scope.$parent.onOk = function () {
        AppDialogService.closeDialog({ ok: true, obj: $scope.obj });
    };

    $scope.onCancel = function () {
        AppDialogService.closeDialog({ ok: false });
    };

    $scope.beforeClose = function () {
        return "";
    };

}]);